/*


 */

package jas.rpc.client;

import jas.rpc.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 *
 *
 */
public class StubFactoryImpl implements StubFactory {
    
    public Object getStub(RemoteObject remote, RpcClient client) {
        Class remoteClass = remote.getClazz();
        try {
            Class clazz = Class.forName(remote.getClazz().getName() + "_Stub");
            Constructor c = clazz.getConstructor(RemoteObject.class, RpcClient.class);
            Object b = c.newInstance(remote, client);
            return b;
        } catch (ClassNotFoundException ex) {
            throw new RuntimeException("Stub class for " + remoteClass.getSimpleName() + " is missing: " + ex.getMessage());
        } catch (NoSuchMethodException ex) {
            throw new RuntimeException("Constructor for stub class " + remoteClass.getSimpleName() + " is missing. It must take a RemoteObject and a RpcClient.");
        } catch (InstantiationException ex) {
            throw new RuntimeException("Stub class " + remoteClass.getSimpleName() + " is abstract.");
        } catch (IllegalAccessException ex) {
            throw new RuntimeException("Stub class " + remoteClass.getSimpleName() + "'s constructor was not public.");
        } catch (InvocationTargetException ex) {
            throw new RuntimeException("Stub class " + remoteClass.getSimpleName() + "'s constructor threw exception: " + ex);
        }
    }
    
}
